﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PagerControl
{
    public partial class FormMain : Form
    {
        List<DataA> data;
        public FormMain()
        {
            InitializeComponent();

            


       
        }
        /// <summary>
        /// 事件处理
        /// </summary>
        /// <param name="obj"></param>
        void pagerControlTest_PageChangeWithObject(object obj)
        {
            PagerLib.PagerControl pager = obj as PagerLib.PagerControl;
            if (pager != null)
            {
                Console.WriteLine(pager.Name);
            }
        }
        /// <summary>
        /// 事件处理
        /// </summary>
        void pagerControlTest_PageChange()
        {
            if (this.pagerControlTest.RecordCount == 0) return;
            FillPagingData();
        }
        /// <summary>
        /// 处理分页
        /// </summary>
        private void FillPagingData()
        {
            //获得总记录条数
            //string sqlcount = "select count(*) from originaldata";
            //pagerControlTest.RecordCount = DBAccess.GetCount(sqlcount);
            //计算显示记录的结束值
            //int end = cur * rows + 1;
            //获得从开始值到结束值的记录
            // string sql = string.Format("select * from (select rownum rn,t.* from (select * from originaldata) t where rownum <{0}) where rn>{1}", end, start);
            // DataTable DT = DBAccess.GetDataTable(sql);


            //获得当前页
            int cur = pagerControlTest.CurrentPage;
            //获得每页显示的记录数
            int rows = pagerControlTest.RowsPerPage;

            pagerControlTest.RecordCount = data.Count();

            //计算显示记录的开始值
            int start = (cur - 1) * rows;
         
            //将得到的记录绑定到DataGridView显示给用户
            var dt = data.Skip((start)).Take(rows).ToList();

            dgvTest.DataSource = dt;
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            data = new List<DataA>();

            data.Add(new DataA { A = "1", B = "2", C = "3", ID = 1 });
            data.Add(new DataA { A = "11", B = "12", C = "13", ID = 2 });
            data.Add(new DataA { A = "21", B = "22", C = "23", ID = 3 });
            data.Add(new DataA { A = "31", B = "32", C = "33", ID = 4 });
            data.Add(new DataA { A = "41", B = "42", C = "43", ID = 5 });
            data.Add(new DataA { A = "51", B = "52", C = "53", ID = 6 });
            data.Add(new DataA { A = "61", B = "62", C = "63", ID = 7 });
            data.Add(new DataA { A = "71", B = "72", C = "73", ID = 8 });
            data.Add(new DataA { A = "81", B = "82", C = "83", ID = 9 });
            data.Add(new DataA { A = "91", B = "92", C = "93", ID = 10 });
            data.Add(new DataA { A = "101", B = "102", C = "103", ID = 11 });
            data.Add(new DataA { A = "111", B = "112", C = "113", ID = 12 });
            data.Add(new DataA { A = "121", B = "122", C = "123", ID = 13 });
            data.Add(new DataA { A = "131", B = "132", C = "133", ID = 14 });
            data.Add(new DataA { A = "141", B = "142", C = "143", ID = 15 });
            data.Add(new DataA { A = "151", B = "152", C = "153", ID = 16 });
            data.Add(new DataA { A = "161", B = "162", C = "163", ID = 17 });
            data.Add(new DataA { A = "171", B = "172", C = "173", ID = 18 });
            data.Add(new DataA { A = "181", B = "182", C = "183", ID = 19 });
            data.Add(new DataA { A = "191", B = "192", C = "193", ID = 20 });
            data.Add(new DataA { A = "201", B = "202", C = "203", ID = 21 });


            FillPagingData();
        }
    }

    public class DataA
    {

        public int ID { get; set; }
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
    }
}
